<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>assign</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ Strings</a> &gt; <a href="assign1.html">assign</a>
  </div>

  <div class="name-format">
    assign
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;string&gt;
  void assign( <strong>size_type</strong> num, const char&amp; val );
  void assign( <a href=
"../iterators.html">input_iterator</a> start, <a href=
"../iterators.html">input_iterator</a> end );
  string&amp; assign( const string&amp; str );
  string&amp; assign( const char* str );
  string&amp; assign( const char* str, <strong>size_type</strong> num );
  string&amp; assign( const string&amp; str, <strong>size_type</strong> index, <strong>size_type</strong> len );
  string&amp; assign( <strong>size_type</strong> num, const char&amp; ch );
</pre>

  <p>The deafult assign() function gives the current string the values
  from <em>start</em> to <em>end</em>, or gives it <em>num</em> copies
  of <em>val</em>.</p>

  <p>In addition to the normal (C++ Lists) <a href=
  "../cpplist/assign.html">assign</a>() functionality that all C++
  containers have, strings possess an assign() function that also
  allows them to:</p>

  <ul>
    <li>assign <em>str</em> to the current string,</li>

    <li>assign the first <em>num</em> characters of <em>str</em> to the
    current string,</li>

    <li>assign a substring of <em>str</em> starting at <em>index</em>
    that is <em>len</em> characters long to the current string,</li>
  </ul>

  <p>For example, the following code:</p>
  <pre class="example-code">
   string str1, str2 = &quot;War and Peace&quot;;
   str1.assign( str2, 4, 3 );
   cout &lt;&lt; str1 &lt;&lt; endl;            
</pre>

  <p>displays</p>
  <pre class="example-code">
   and          
</pre>

  <p>This function will destroy the previous contents of the
  string.</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    (C++ Lists) <a href="../cpplist/assign.html">assign</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
